package com.controller;

import com.controller.vo.ResultVO;
import com.dto.OrderDto;
import com.dto.OrderItemDto;
import com.dto.OrderRequestDTO;
import com.entity.Order;
import com.entity.OrderItem;
import com.github.pagehelper.PageInfo;
import com.service.OrderService;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 *@ClassName OrderController
 *@Description  订单控制层
 *@Author Kx
 *@Date 2025/5/27 19:48
 *@Version 1.0
 */
@RestController
public class OrderController extends BaseController {
    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * @author: kx
     * @date: 2025/6/3 19:38
     * @param orderRequestDTO : 订单数据传输对象
     * @return com.controller.vo.ResultVO<com.entity.Order>
     * @description: 添加订单信息
    */
    @PostMapping("/order/addOrder")
    public ResultVO<Order> addOrder(@RequestBody OrderRequestDTO orderRequestDTO) {
        if (orderRequestDTO.getMeans() != null) {
            if (orderRequestDTO.getMeans() == 0) {
                // 添加订单(通过购物车购买)
                orderService.addOrderTrolley(orderRequestDTO.getOrder(), orderRequestDTO.getOrderItems());
            } else {
                // 添加订单(直接购买)
                orderService.addOrder(orderRequestDTO.getOrder(), orderRequestDTO.getOrderItems());
            }
        } else {
            throw new IllegalArgumentException("means参数不能为空");
        }
        return success(orderRequestDTO.getOrder());
    }

    /**
     * @author: kx
     * @date: 2025/5/27 20:00
     * @param orderItem : 订单商品项
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @description: 添加订单商品项
    */
    @PostMapping("/order/addOrderItem")
    public ResultVO<String> addOrderItem(@RequestBody OrderItem orderItem) {
        orderService.addOrderItem(orderItem);
        return success("添加成功");
    }

    /**
     * @author: kx
     * @date: 2025/5/28 10:43
     * @param userId : 用户编号
     * @return com.controller.vo.ResultVO<java.util.List<com.dto.OrderItemDto>>
     * @description: 根据用户编号查询购物车信息
    */
    @GetMapping("/order/findByUserId")
    public ResultVO<List<OrderItemDto>> findByUserId(@RequestParam("userId") Integer userId) {
        List<OrderItemDto> orderItemDtoList = orderService.findByUserId(userId);
        return success(orderItemDtoList);
    }

    /**
     * @author: kx
     * @date: 2025/5/28 10:55
     * @param orderItem : 订单商品信息
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @description: 修改订单商品信息
    */
    @PostMapping("/order/updateOrderItem")
    public ResultVO<String> updateOrderItem(@RequestBody OrderItem orderItem) {
        orderService.updateOrderItem(orderItem);
        return success("修改成功");
    }

    /**
     * @author: kx
     * @date: 2025/6/19 15:08
     * @param itemId : 订单商品编号
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @description: 删除订单商品信息
    */
    @PostMapping("/order/deleteOrderItem")
    public ResultVO<String> deleteOrderItem(@RequestParam("itemId") Integer itemId) {
        orderService.deleteOrderItem(itemId);
        return success("删除成功");
    }

    /**
     * @param orderStatus : 订单状态
     * @param userId      : 用户编号
     * @return com.controller.vo.ResultVO<java.util.List < com.dto.OrderDto>>
     * @author: kx
     * @date: 2025/5/28 15:05
     * @description: 根据订单状态查询和用户编号订单信息
     */
    @GetMapping("/order/findByOrderStatus")
    public ResultVO<List<OrderDto>> findByOrderStatus(@RequestParam("orderStatus") Integer orderStatus, @RequestParam("userId") Integer userId) {
        // 查询所有订单信息
        List<OrderDto> orderDtoS = orderService.findByOrderStatus(orderStatus, userId);
        return success(orderDtoS);
    }

    /**
     * @param orderId      : 订单编号
     * @param orderStatus  : 订单状态
     * @param cancelReason : 取消原因
     * @return com.controller.vo.ResultVO<java.lang.String>
     * @author: kx
     * @date: 2025/5/28 16:08
     * @description: 根据订单编号修改订单状态
     */
    @PostMapping("/order/updateOrderStatus")
    public ResultVO<String> updateOrderStatus(@RequestParam("orderId") String orderId, @RequestParam("orderStatus") Integer orderStatus, @RequestParam(value = "cancelReason", required = false) String cancelReason) {
        // 判断订单状态是否为取消状态
        if (orderStatus != 4) {
            cancelReason = null;
        }
        orderService.updateOrderStatus(orderId, orderStatus, cancelReason);
        return success("修改成功");
    }

    /**
     * @param yearMonth : 年月份
     * @return com.controller.vo.ResultVO<java.math.BigDecimal>
     * @author: kx
     * @date: 2025/6/11 9:13
     * @description: 根据年月份查询该年月份的销售额
     */
    @GetMapping("/order/findSalesByYearAndMonth")
    public ResultVO<BigDecimal> findSalesByYearAndMonth(@RequestParam("yearMonth") String yearMonth) {
        // 获取该年月份的销售额
        BigDecimal bigDecimal = orderService.findSalesByYearAndMonth(yearMonth);
        return success(bigDecimal);
    }

    /**
     * @author: kx
     * @date: 2025/6/11 20:22
     * @param year : 年份
     * @return com.controller.vo.ResultVO<java.util.Map<java.lang.Integer,java.math.BigDecimal>>
     * @description: 根据年份获取该年份下的每月销售额
    */
    @GetMapping("/order/monthlySales")
    public ResultVO<Map<Integer, BigDecimal>> getMonthlySalesByYear(@RequestParam("year") String year) {
        Map<Integer, BigDecimal> monthlySalesByYear = orderService.getMonthlySalesByYear(year);
        return success(monthlySalesByYear);
    }

    /**
     * @author: kx
     * @date: 2025/6/16 15:40
     * @param pageNum : 页码
     * @param pageSize : 每页显示数量
     * @return com.controller.vo.ResultVO<com.github.pagehelper.PageInfo<com.entity.Order>>
     * @description: 分页查询所有订单信息
    */
    @GetMapping("/order/findAllOrders")
    public ResultVO<PageInfo<OrderDto>> findAllOrders(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return success(orderService.findAllOrders(pageNum, pageSize));
    }

    /**
     * @author: kx
     * @date: 2025/6/19 15:42
     * @param userKeyword : 用户名关键词
     * @param pageNum : 页码
     * @param pageSize : 每页显示数量
     * @return com.controller.vo.ResultVO<com.github.pagehelper.PageInfo<com.dto.OrderDto>>
     * @description: 根据用户名关键词分页查询用户订单信息
    */
    @GetMapping("/order/findOrdersByUserId")
    public ResultVO<PageInfo<OrderDto>> findOrdersByUserId(@RequestParam("userKeyword") String userKeyword, @RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return success(orderService.findOrdersByUserId(userKeyword, pageNum, pageSize));
    }
}
